package com.easy.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.Order;
import com.easy.common.bean.OrderItem;
import com.easy.common.service.IOrderService;
import com.easy.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    // 插入订单
    @PostMapping("/")
    public ResultData insert(@RequestBody Order order) {
        Order data = orderService.insert(order);
        return ResultData.success(data);
    }

    // 更新订单
    @PutMapping("/")
    public ResultData update(@RequestBody Order order) {
        Order data = orderService.update(order);
        return ResultData.success(data);
    }

    // 完成订单
    @PutMapping("/transact/{id}")
    public ResultData transact(@PathVariable Integer id) {
        Order data = orderService.transact(id);
        return ResultData.success(data);
    }

    // 取消订单
    @PutMapping("/cancel/{id}")
    public ResultData cancel(@PathVariable Integer id) {
        Order data = orderService.cancel(id);
        return ResultData.success(data);
    }

    // 根据 ID 获取订单
    @GetMapping("/{id}")
    public ResultData getById(@PathVariable Integer id) {
        Order data = orderService.getById(id);
        return ResultData.success(data);
    }

    // 根据用户 ID 分页获取订单列表
    @GetMapping("/user/{userid}")
    public ResultData getListByUserID(Page page, @PathVariable Integer userid) {
        IPage<Order> data = orderService.getListByUserID(page, userid);
        return ResultData.success(data);
    }

    // 根据客户 ID 分页获取订单列表
    @GetMapping("/customer/{customerid}")
    public ResultData getListByCustomerID(Page page, @PathVariable Integer customerid) {
        IPage<Order> data = orderService.getListByCustomerID(page, customerid);
        return ResultData.success(data);
    }

    // 插入订单明细
    @PostMapping("/item")
    public ResultData insertItem(@RequestBody OrderItem orderItem) {
        OrderItem data = orderService.insertItem(orderItem);
        return ResultData.success(data);
    }

    // 更新订单明细
    @PutMapping("/item")
    public ResultData updateItem(@RequestBody OrderItem orderItem) {
        OrderItem data = orderService.updateItem(orderItem);
        return ResultData.success(data);
    }

    // 删除订单明细
    @DeleteMapping("/item/{id}")
    public ResultData deleteItem(@PathVariable Integer id) {
        int result = orderService.deleteItem(id);
        if (result >= 1) {
            return ResultData.success("200", "删除成功", null);
        } else {
            return ResultData.fail();
        }
    }
}